﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MathNet.Numerics.Statistics;
namespace QuantitativeInvestment.Bean
{
    class PortfolioManager
    {
       public  List<Portfolio> soldPortfolioList;
       public List<Portfolio> stockPoolPortfolioList;
       public List<Portfolio> holdPortfolioList;
       public int maxHoldPeriod;
       public string benchMarkName;
       public  double accumulatedExcessReturn=0;
       public  double accumulatedReturn=0;
       public double excessIncreaseVariance;
       public double avgIncreaseVariance;

       //胜率
       public double successRate;

       public double avgReturn;

       public double avgYearReturn;

       public double avgYearExcessReturn;
       public double avgExcessReturn;

       public void caculateStatics()
       {
           Accumulator excessIncreases = new Accumulator();
           Accumulator avgIncreases = new Accumulator();
          
           accumulatedExcessReturn = 0;
           accumulatedReturn = 0;
           int successNum = 0;
           for (int i = 0; i < soldPortfolioList.Count; i++)
           {
               excessIncreases.Add(soldPortfolioList[i].excessIncrease);
               avgIncreases.Add(soldPortfolioList[i].averageIncrease);
       
               accumulatedExcessReturn = (soldPortfolioList[i].excessIncrease + 1) * (accumulatedExcessReturn + 1) - 1;
               accumulatedReturn = (soldPortfolioList[i].averageIncrease + 1) * (accumulatedReturn + 1) - 1;

               if (soldPortfolioList[i].excessIncrease >= 0)
               {
                   successNum++;
               }
           }

           if (soldPortfolioList.Count > 0)
           {
               excessIncreaseVariance = excessIncreases.Variance;
               avgIncreaseVariance = avgIncreases.Variance;
               avgExcessReturn = excessIncreases.Mean;
               avgReturn = avgIncreases.Mean;
               avgYearReturn = Math.Pow((1 + avgReturn), 250 / this.maxHoldPeriod) - 1;
               avgYearExcessReturn = Math.Pow((1 + avgExcessReturn), 250.0 / this.maxHoldPeriod) - 1;
               successRate = successNum * 1.0 / soldPortfolioList.Count;
           }

           
          
       }



    }
}
